package com.dsx.test.async;

import com.dsx.test.async.service.AsyncTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.rmi.log.ReliableLog;

/**
 * @author : tianwenqing
 * @version : 1.0
 * @date : 2019-09-26 19:42
 * @description : @Async 异步测试
 **/
@RestController
@RequestMapping("/async")
@Slf4j
public class AsyncTestController {

    @Autowired
    private AsyncTestService asyncTestService;

    @Autowired(required = false)
    @Qualifier(value = "taskExecutor")
    private TaskExecutor taskExecutor;

    @GetMapping("test")
    public void test(){
        log.info("==========1");
        asyncTestService.testsync();
        log.info("==========4");
    }

    @GetMapping("test1")
    public void test1(){
        log.info("==========1");
        asyncTestService.testAsync();
        log.info("==========4");
    }

    @GetMapping("test2")
    public void test2(){
        log.info("==========1");
        int y = 0 ;
        for (int i = 0; i < 1000; i++) {
            try {
                asyncTestService.testAsync1();
            } catch (Exception e) {
                e.printStackTrace();
                log.error("丢弃任务数"+(++y));
            }
        }
        log.info("==========结束");

    }

    @GetMapping("test3")
    public void test3(){
        ThreadPoolTaskExecutor tpe = ((ThreadPoolTaskExecutor) taskExecutor);
        log.info("----------线程池中的线程数量：{}",tpe.getThreadPoolExecutor().getPoolSize());
        log.info("----------线程池中曾经创建过的最大线程数：{}",tpe.getThreadPoolExecutor().getLargestPoolSize());
        log.info("----------线程池中活跃的线程数：{}",tpe.getThreadPoolExecutor().getActiveCount());
        log.info("----------线程池中需要执行的任务数量：{}",tpe.getThreadPoolExecutor().getTaskCount());
        log.info("----------线程池中完成的任务数量：{}",tpe.getThreadPoolExecutor().getCompletedTaskCount());
    }

}
